import { defineStore } from "pinia";
import { Conversation, Profile } from 'tim-js-sdk';
import { TextMessage, TextMessageEvent } from "./plugins/TIM-plugin/type";

export const useTIMStore = defineStore('chat',{
  state(){
    return{
      conversationLIst:[] as Partial<Conversation>[],
      selectedKeys:[] as string[],
      historyMessage:[] as TextMessage[]
    }
  },
  // TIM初始化参数
  TIMOptions(){
    return {
      SDKAppID:1400789552
    }
  },
  actions:{
    subscribeMessage(event:TextMessageEvent){},
    async getSessionList(){
      const {data:{conversationList}} = await this.timCore.tim?.getConversationList()
      this.conversationLIst = conversationList
      if(conversationList.length === 0 && this.timCore.userID !=='admin') {
        this.conversationLIst.push({
          lastMessage:{},
          userProfile:{
            userID:'admin'
          } as Profile
        })
      }
      const userId = this.conversationLIst[0]?.userProfile?.userID
      if(userId){
        this.selectedKeys = [userId]
        this.getMessageHistoryList(userId)
      }
    },
    async getMessageHistoryList(userId:string){
      const data = await this.timCore.tim?.getMessageList({
        conversationID:`C2C${userId}`
      })
      this.historyMessage = data.data.messageList
    }
  }
})